package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import objects.Order_ZSD_Child;
import objects.Order_ZSD_Parent;

/**
 * Created by Monkey on 2017/3/30.
 * 注射单适配器
 */

public class ZSDListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private Map<Order_ZSD_Parent, List<Order_ZSD_Child>> dataSets;
    private List<Order_ZSD_Parent> parentsList;
    private List<Boolean> isChoiced;

    public ZSDListAdapter(Context context) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setDataSets(Map<Order_ZSD_Parent, List<Order_ZSD_Child>> dataSets) {
        this.dataSets = dataSets;
        parentsList = new ArrayList<>();
        isChoiced = new ArrayList<>();
        getParentList();
        Collections.sort(parentsList, new Comparator<Order_ZSD_Parent>() {
            public int compare(Order_ZSD_Parent arg0, Order_ZSD_Parent arg1) {
                return arg0.getTMBH().compareTo(arg1.getTMBH());
            }
        });
    }

    private void getParentList() {
        Set set = dataSets.keySet();
        for (Object aSet : set) {
            parentsList.add((Order_ZSD_Parent) aSet);
            isChoiced.add(false);
        }
    }

    public int getChoicedCount() {
        int count = 0;
        if (isChoiced!=null){
            for (boolean choiced : isChoiced) {
                if (choiced) {
                    count++;
                }
            }
        }
        return count;
    }

    public boolean getChoicedItem(int pos) {
        return isChoiced.get(pos);
    }

    public void setChoicedItem(int pos, boolean choiced) {
        isChoiced.set(pos, choiced);
    }

    @Override
    public int getGroupCount() {
        if (parentsList != null && parentsList.size() > 0) {
            return parentsList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int i) {
        if (parentsList != null && parentsList.get(i) != null && dataSets.get(parentsList.get(i)) != null) {
            return dataSets.get(parentsList.get(i)).size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int i) {
        if (parentsList != null && parentsList.get(i) != null) {
            return parentsList.get(i);
        }
        return null;
    }

    @Override
    public Object getChild(int parentPos, int childPos) {
        if (parentsList != null && dataSets.get(parentsList.get(parentPos)) != null) {
            return dataSets.get(parentsList.get(parentPos)).get(childPos);
        }
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int parentPos, boolean b, View converView, ViewGroup viewGroup) {
        ParentViewHolder holder;
        if (converView == null) {
            holder = new ParentViewHolder();
            converView = inflater.inflate(R.layout.item_zsd_parent, viewGroup, false);
            holder.zs_cb = (CheckBox) converView.findViewById(R.id.zs_cb);
            holder.zs_tmbh = (TextView) converView.findViewById(R.id.zs_tmbh_text);
            holder.zs_ypyf = (TextView) converView.findViewById(R.id.zs_ypyf_text);
            holder.zs_sypc = (TextView) converView.findViewById(R.id.zs_sypc_text);
            holder.zs_zssj = (TextView) converView.findViewById(R.id.zs_zssj_text);
            holder.zs_hdzt = (TextView) converView.findViewById(R.id.zs_hdzt_text);
            holder.zs_hdsj = (TextView) converView.findViewById(R.id.zs_hdsj_text);
            holder.zs_hdry = (TextView) converView.findViewById(R.id.zs_hdry_text);
            holder.linearLayout = (LinearLayout) converView.findViewById(R.id.zs_hd_linear);
            converView.setTag(holder);
        } else {
            holder = (ParentViewHolder) converView.getTag();
        }
        holder.zs_cb.setChecked(isChoiced.get(parentPos));
//        if (isChoiced.get(parentPos)) {
//            converView.setBackground(mContext.getResources().getDrawable(R.drawable.corner_blue_deep));
//        } else {
//            converView.setBackground(mContext.getResources().getDrawable(R.drawable.corner_blue_light));
//        }
        holder.zs_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isChoiced.set(parentPos, isChecked);
            }
        });
        if (parentsList != null && parentsList.size() > 0) {
            Order_ZSD_Parent orderZsdParent = parentsList.get(parentPos);
            if (orderZsdParent.getTMBH() != null) {
                holder.zs_tmbh.setText(orderZsdParent.getTMBH());
            }
            if (orderZsdParent.getYFMC() != null) {
                holder.zs_ypyf.setText(orderZsdParent.getYFMC());
            }
            if (orderZsdParent.getSYPC() != null) {
                holder.zs_sypc.setText(orderZsdParent.getSYPC());
            }
            if (orderZsdParent.getZSSJ() != null) {
                holder.zs_zssj.setText(orderZsdParent.getZSSJ().substring(5, 16));
            }
            if (orderZsdParent.getHDBZ() != null && orderZsdParent.getHDBZ().equals("1")) {
                holder.zs_hdzt.setText("已核对");
                holder.zs_hdzt.setTextColor(mContext.getResources().getColor(R.color.colorBlue));
                holder.linearLayout.setVisibility(View.VISIBLE);
                if (orderZsdParent.getHDSJ() != null) {
                    String text = mContext.getString(R.string.syd_text_1) + orderZsdParent.getHDSJ().substring(0, 19);
                    holder.zs_hdsj.setText(text);
                }
                if (orderZsdParent.getYHXM() != null) {
                    String text = mContext.getString(R.string.syd_text_2) + orderZsdParent.getYHXM();
                    holder.zs_hdry.setText(text);
                }
            } else {
                holder.zs_hdzt.setText("未核对");
                holder.zs_hdzt.setTextColor(mContext.getResources().getColor(R.color.colorRed));
                holder.linearLayout.setVisibility(View.GONE);
            }
        }
        return converView;
    }

    @Override
    public View getChildView(int parentPos, int childPos, boolean b, View converView, ViewGroup viewGroup) {
        ChildViewHolder holder;
        List<Order_ZSD_Child> list = dataSets.get(parentsList.get(parentPos));
        Collections.sort(list, new Comparator<Order_ZSD_Child>() {
            public int compare(Order_ZSD_Child arg0, Order_ZSD_Child arg1) {
                return arg0.getYZXH().compareTo(arg1.getYZXH());
            }
        });
        if (converView == null) {
            holder = new ChildViewHolder();
            converView = inflater.inflate(R.layout.item_zsd_child, viewGroup, false);
            holder.zs_yzxh = (TextView) converView.findViewById(R.id.zs_yzxh_text);
            holder.zs_ycjl = (TextView) converView.findViewById(R.id.zs_ycjl_text);
            holder.zs_jldw = (TextView) converView.findViewById(R.id.zs_jldw_text);
            holder.zs_ycsl = (TextView) converView.findViewById(R.id.zs_ycsl_text);
            holder.zs_sldw = (TextView) converView.findViewById(R.id.zs_sldw_text);
            converView.setTag(holder);
        } else {
            holder = (ChildViewHolder) converView.getTag();
        }

        if (list.size() > 0) {
            Order_ZSD_Child orderZsdChild = list.get(childPos);
            if (orderZsdChild.getYZMC() != null) {
                holder.zs_yzxh.setText(orderZsdChild.getYZMC());
            }
            if (orderZsdChild.getYCJL() != null) {
                holder.zs_ycjl.setText(orderZsdChild.getYCJL());
            }
            if (orderZsdChild.getJLDW() != null) {
                holder.zs_jldw.setText(orderZsdChild.getJLDW());
            }
            if (orderZsdChild.getYCSL() != null) {
                holder.zs_ycsl.setText(orderZsdChild.getYCSL());
            }
            if (orderZsdChild.getSLDW() != null) {
                holder.zs_sldw.setText(orderZsdChild.getSLDW());
            }
        }
        return converView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    private class ParentViewHolder {
        CheckBox zs_cb;
        TextView zs_tmbh;
        TextView zs_ypyf;
        TextView zs_sypc;
        TextView zs_hdzt;
        TextView zs_hdsj;
        TextView zs_hdry;
        TextView zs_zssj;
        LinearLayout linearLayout;
    }

    private class ChildViewHolder {
        TextView zs_yzxh;
        TextView zs_ycjl;
        TextView zs_jldw;
        TextView zs_ycsl;
        TextView zs_sldw;
    }

    public void updateBackGroup(int position, boolean choiced) {
        isChoiced.set(position, choiced);
    }
}
